package com.leyou.page.mq;

import com.gitee.yongzhuzl.commonutil.util.empty.EmptyUtil;
import com.leyou.page.service.PageService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * description: 静态页微服务对商品监听
 * @author 朱正磊
 * @date 2021-04-14 13:47:10
 */
@Component
public class ItemListener {

    @Resource
    private PageService pageService;

    /**
     * description: 监听商品微服务新增或者修改商品
     * @author 朱正磊
     * @date 2021-04-14 00:59:37
     * @param spuId spu的id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "page.item.insert.or.update.queue", durable = "true"),
            exchange = @Exchange(name = "ly.item.exchange", type = ExchangeTypes.TOPIC),
            key = {"item.insert", "item.update"}
    ))
    public void listenInsertOrUpdateItem(Long spuId) {
        if (EmptyUtil.isEmpty(spuId)) {
            // 如果商品id为空，直接返回什么都不做
            return;
        }

        // 处理消息，重新生成相关静态页
        pageService.createHtml(spuId);
    }

    /**
     * description: 监听商品微服务删除商品
     * @author 朱正磊
     * @date 2021-04-14 00:59:37
     * @param spuId spu的id
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "page.item.delete.queue", durable = "true"),
            exchange = @Exchange(name = "ly.item.exchange", type = ExchangeTypes.TOPIC),
            key = {"item.delete"}
    ))
    public void listenDeleteItem(Long spuId) {
        if (EmptyUtil.isEmpty(spuId)) {
            // 如果商品id为空，直接返回什么都不做
            return;
        }

        // 处理消息，删除相关静态页
        pageService.deleteHtml(spuId);
    }

}
